package com.zxh.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxh.mapper.TbBrandMapper;
import com.zxh.sellergoods.service.BrandService;
import com.zxh.zeyigou.pojo.PageResult;
import com.zxh.zeyigou.pojo.TbBrand;
import com.zxh.zeyigou.pojo.TbBrandExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Auther zxh
 * @Date 2020/11/16下午 8:16
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper brandMapper;
    //1.查询所有品牌
    @Override
    public List<TbBrand> findAll() {
        return brandMapper.selectAll();
    }
    //2.分页查询
    @Override
    public PageResult<TbBrand> findBypage(int page, int pageSize) {
        //2.1)开始分页
        PageHelper.startPage(page,pageSize);
        //2.2)进行实例查询
        TbBrandExample example = new TbBrandExample();
        List<TbBrand> tbBrands = brandMapper.selectByExample(example);
        //2.3)转换为Page<TbBrand>对象
        Page<TbBrand> brandPage = (Page<TbBrand>) tbBrands;
        //2.4)返回PageResult对象
        return new PageResult<TbBrand>(brandPage.getTotal(),brandPage.getResult());
    }

    //3.条件查询带分页
    @Override
    public PageResult<TbBrand> search(int page, int pageSize, TbBrand brand) {
        //3.1)开始分页
        PageHelper.startPage(page,pageSize);
        //3.2)进行实例查询
        TbBrandExample example = new TbBrandExample();
        //3.2.1)添加查询条件
        TbBrandExample.Criteria criteria = example.createCriteria();
        if(brand != null){
            if(StringUtils.isNotBlank(brand.getName())){
                criteria.andNameLike("%" + brand.getName() + "%");
            }
            if(StringUtils.isNotBlank(brand.getFirstChar())){
                criteria.andFirstCharEqualTo(brand.getFirstChar());
            }
        }
        //3.2.3)进行实例查询
        List<TbBrand> tbBrands = brandMapper.selectByExample(example);
        //3.3)转换为Page<TbBrand>对象
        Page<TbBrand> brandPage = (Page<TbBrand>) tbBrands;
        //3.4)返回PageResult对象
        return new PageResult<TbBrand>(brandPage.getTotal(),brandPage.getResult());
    }

    //4.添加品牌
    @Override
    public void add(TbBrand brand) {
        brandMapper.insert(brand);
    }


    //5.修改品牌
    @Override
    public void update(TbBrand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    //6.删除品牌
    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            brandMapper.deleteByPrimaryKey(id);
        }
    }
}
